#include "core.h"
#include <stdbool.h>

// CPU特性检测
#if defined(ARCH_X86)
#include <cpuid.h>
#elif defined(ARCH_ARM)
#include <sys/auxv.h>
#include <asm/hwcap.h>
#endif

bool cpu_supports_avx2(void) {
#if defined(ARCH_X86)
    unsigned int eax, ebx, ecx, edx;
    __get_cpuid(1, &eax, &ebx, &ecx, &edx);
    return (ecx & bit_AVX) && (ebx & bit_AVX2);
#else
    return false;
#endif
}

bool cpu_supports_neon(void) {
#if defined(ARCH_ARM)
    return getauxval(AT_HWCAP) & HWCAP_NEON;
#else
    return false;
#endif
}

bool cpu_supports_sve(void) {
#if defined(ARCH_ARM) && defined(HWCAP2_SVE)
    return getauxval(AT_HWCAP2) & HWCAP2_SVE;
#else
    return false;
#endif
}

// 矩阵乘法分发
ErrorCode matrix_multiply(const Matrix* a, const Matrix* b, Matrix** result) {
    if (!a || !b) return ERR_NULL_PTR;
    if (a->cols != b->rows) return ERR_DIMENSION_MISMATCH;
    
    // 根据架构和CPU特性选择最优实现
    #if defined(ARCH_X86)
        if (cpu_supports_avx2()) {
            return matrix_multiply_avx2(a, b, result);
        }
    #elif defined(ARCH_ARM)
        if (cpu_supports_sve()) {
            return matrix_multiply_sve(a, b, result);
        } else if (cpu_supports_neon()) {
            return matrix_multiply_neon(a, b, result);
        }
    #endif
    
    // 回退到通用实现
    return matrix_multiply_generic(a, b, result);
}

// 图像灰度转换分发
ErrorCode image_grayscale(const Image* src, Image** dest) {
    if (!src) return ERR_NULL_PTR;
    if (src->channels != 3) return ERR_INVALID_CHANNELS;
    
    // 根据架构选择最优实现
    #if defined(ARCH_ARM)
        if (cpu_supports_neon()) {
            return image_grayscale_neon(src, dest);
        }
    #endif
    
    // 回退到通用实现
    return image_grayscale_generic(src, dest);
}




















